package com.dc.datacollect.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dc.datacollect.constant.PermitNameConstant;
import com.dc.datacollect.constant.type.UserType;
import com.dc.datacollect.entity.Permit;
import com.dc.datacollect.mapper.PermitMapper;
import com.dc.datacollect.service.CollectService;
import com.dc.datacollect.service.PermitService;
import com.dc.datacollect.helpers.SessionProvide;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhuangjie
 * @description 针对表【permit】的数据库操作Service实现
 * @createDate 2023-06-20 09:35:35
 */
@Service
public class PermitServiceImpl extends ServiceImpl<PermitMapper, Permit>
        implements PermitService {

    @Autowired
    private CollectService collectService;

    @Override
    public List<Permit> getPermitTree(Short type) {
        LambdaQueryWrapper<Permit> permitQueryWrapper = new LambdaQueryWrapper<>();
        permitQueryWrapper.eq(Permit::getType,type).or(wrapper->wrapper.eq(Permit::getType, UserType.COMMON));
        List<Permit> typePermitList = this.list(permitQueryWrapper);
        Map<Long, Permit> typePermitMap = typePermitList.stream().collect(Collectors.toMap(permit -> permit.getId(), permit -> permit));
        typePermitList.forEach(permit -> {
            Long parentId = permit.getParent();
            if ( parentId == null) {
                return;
            }
            // 查消息
            addPermitMessageCount(permit);
            Permit parent = typePermitMap.get(parentId);
            if (parent != null) {
                if (parent.getChildren() == null ) {
                    parent.setChildren(new ArrayList<>());
                }
                parent.getChildren().add(permit);
            }
            // 到这里肯定不是第一级的permit,把自己从第一级的map中删除
            typePermitMap.remove(permit.getId());
        });
        List<Permit> permitTree = typePermitMap.values().stream().collect(Collectors.toList());
        permitTree.forEach(permit->{
            List<Permit> childrenPermits = permit.getChildren();
            // 如果元素为空或为0，不排序
            if (childrenPermits == null || childrenPermits.size() == 0) {
                return;
            }
            // 定义一个比较器，按照某个属性值进行排序
            Comparator<Permit> comparator = new Comparator<Permit>() {
                @Override
                public int compare(Permit p1, Permit p2) {
                    // 比较 p1 和 p2 的某个属性值，并返回比较结果-降序
                    return p2.getSort().compareTo(p1.getSort());
                }
            };
            // 使用 Collections.sort() 方法进行排序
            Collections.sort(childrenPermits, comparator);
        });

        return permitTree;
    }

    private void addPermitMessageCount(Permit permit) {
        if (PermitNameConstant.IN_PROGRESS.equals(permit.getName())) {
            // 给待提交permit加messageCount
            permit.setMessageCount(collectService.selectWaitSubmitCount(SessionProvide.getLoginObject().getId()));
            System.out.println("添加了message count="+permit.getMessageCount());
        }
    }



}




